import { Tools } from 'src/app/common/tools/tools';
import { AfterViewInit, Component, OnInit, OnDestroy, ViewChild, ElementRef } from '@angular/core';
import { BaseFieldComponent } from '../base-field/base-field.component';
import { NzMessageService } from 'ng-zorro-antd';
import { BaseService, DataRegisterService, PageService } from 'src/app/services';
import { EventManagerService } from 'src/app/services/event-manager.service';
import { OrganizationService } from 'src/app/services/organization.service';
import { UploadService } from 'src/app/services/upload.service';
import { EngineService } from '../../../services/engine.service';
import { PermitConfig } from '../../../models';

@Component({
  selector: 'zepride-bid-evaluation',
  templateUrl: './bid-evaluation.component.html',
  styleUrls: ['./bid-evaluation.component.less', '../bid-opening/bid-opening.component.less']
})
export class BidEvaluationComponent extends BaseFieldComponent implements OnInit, AfterViewInit, OnDestroy {
  isfullscreen: boolean;
  fields = [];
  packageList: any[];
  permitConfig: PermitConfig[] = [
    { mode: 'query', permitId: '查看', permitName: '', authObjectId: '' },
    { mode: 'edit', permitId: '编辑', permitName: '', authObjectId: '' }
  ];
  @ViewChild('contentFrame') set contentFrame(ref: ElementRef<HTMLFrameElement>) {
    this.contentFrameRef = ref;
  }
  contentFrameRef: ElementRef<HTMLFrameElement>;
  @ViewChild('container') container: ElementRef<HTMLDivElement>;

  constructor(
    protected engineService: EngineService,
    protected baseSvr: BaseService,
    protected eventSvr: EventManagerService,
    protected orgSvr: OrganizationService,
    public uploadService: UploadService,
    protected msg: NzMessageService,
    protected pageSvr: PageService,
    protected dataRegisterService: DataRegisterService
  ) {
    super(engineService, baseSvr, eventSvr, orgSvr, uploadService, msg, dataRegisterService);
  }

  ngOnDestroy(): void {
    if (this.modelChange$) {
      this.modelChange$.unsubscribe();
    }
  }

  ngOnInit(): void {
    this.componentData.extends || (this.componentData.extends = {});
    if (this.componentData.extends.fields && this.componentData.extends.fields.length) {
      this.fields = this.componentData.extends.fields;
      if (this.fields && this.fields.length) {
        this.fields[0].selected = true;
      }
    }

    if (this.bindData) {
      this.packageList = this.bindData;
      this.previewFile(this.packageList[0]);
    }
    // else {
    //   this.previewFile(null);
    // }

    this.registerModelChange();
  }

  ngAfterViewInit() {
    setTimeout(() => {
      this.setAuthorityData();
    }, 0);
  }

  registerModelChange() {
    this.modelChange$ = this.engineService.dataModelChange$.subscribe(data => {
      if (data.identifyId !== this.componentData.identifyId) {
        this.packageList = this.formateData();
        if (this.packageList && this.packageList.length) {
          this.packageList[0].selected = true;
        }
      }
    });
  }


  private formateData() {
    return this.bindData;
  }

  fullScreen() {
    if (!this.container || !this.container.nativeElement) {
      return;
    }
    const ele = this.container.nativeElement;
    if (this.isfullscreen) {
      document.exitFullscreen();
    } else {
      ele.requestFullscreen();
    }
    this.isfullscreen = !this.isfullscreen;
  }

  async downloadFile() {
    const feildItem = this.fields.find(field => field.selected);
    const fileData: any = await this.pageSvr.makeFile(this.dataModel, feildItem.fileUrl, 4, 'excel');
    window.open(fileData.data);
  }

  tabClick(item: { selected: boolean; }) {
    this.fields.map(x => x.selected = false);
    item.selected = true;
    this.previewFile(item);
  }

  pkgSelect(item: { selected: boolean; }) {
    this.packageList.map(x => x.selected = false);
    item.selected = true;
    this.previewFile(item);
  }

  previewFile(item) {
    setTimeout(async () => {
      if (this.contentFrameRef && this.contentFrameRef.nativeElement) {
        const htmlElement = this.contentFrameRef.nativeElement;
        const feildItem = this.fields.find(x => x.selected);
        const data = JSON.parse(JSON.stringify(this.dataModel));
        data[this.componentData.bindField] = item;
        const r: any = await this.pageSvr.makeFile(data, feildItem.fileUrl, 4, 'excel');
        htmlElement.src = `https://view.officeapps.live.com/op/view.aspx?src=${r.data}`;
      }
    }, 1000);
  }
}
